package com.ruoyi.dl.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.file.FileUploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dl.domain.DlCoupons;
import com.ruoyi.dl.service.IDlCouponsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 优惠券Controller
 * 
 * @author ruoyi
 * @date 2020-12-28
 */
@Controller
@RequestMapping("/dl/coupons")
public class DlCouponsController extends BaseController
{
    private String prefix = "dl/coupons";

    @Autowired
    private IDlCouponsService dlCouponsService;

    @RequiresPermissions("dl:coupons:view")
    @GetMapping()
    public String coupons()
    {
        return prefix + "/coupons";
    }

    /**
     * 查询优惠券列表
     */
    @RequiresPermissions("dl:coupons:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DlCoupons dlCoupons)
    {
        startPage();
        List<DlCoupons> list = dlCouponsService.selectDlCouponsList(dlCoupons);
        return getDataTable(list);
    }

    /**
     * 导出优惠券列表
     */
    @RequiresPermissions("dl:coupons:export")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DlCoupons dlCoupons)
    {
        List<DlCoupons> list = dlCouponsService.selectDlCouponsList(dlCoupons);
        ExcelUtil<DlCoupons> util = new ExcelUtil<DlCoupons>(DlCoupons.class);
        return util.exportExcel(list, "coupons");
    }

    /**
     * 新增优惠券
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存优惠券
     */
    @RequiresPermissions("dl:coupons:add")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DlCoupons dlCoupons)
    {
        return toAjax(dlCouponsService.insertDlCoupons(dlCoupons));
    }

    /**
     * 修改优惠券
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DlCoupons dlCoupons = dlCouponsService.selectDlCouponsById(id);
        mmap.put("dlCoupons", dlCoupons);
        return prefix + "/edit";
    }

    /**
     * 修改保存优惠券
     */
    @RequiresPermissions("dl:coupons:edit")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DlCoupons dlCoupons)
    {
       /* //判断活动开始和结束时间
        if (dlCoupons.getEndTime().before(dlCoupons.getStartTime())){
            return error("活动结束时间不能比开始时间早！");
        }*/
        return toAjax(dlCouponsService.updateDlCoupons(dlCoupons));
    }

    /**
     * 删除优惠券
     */
    @RequiresPermissions("dl:coupons:remove")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dlCouponsService.deleteDlCouponsByIds(ids));
    }

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public Map<String, String> upload(@RequestParam("file") MultipartFile file) {
        Map<String, String> map = new HashMap<>();
        try {
            String avatar = FileUploadUtils.upload(file);
            map.put("data", avatar);
            map.put("code", "0");
        } catch (Exception e) {
            map.put("data", e.getMessage().toString());
            map.put("code", "0");
        }
        return map;
    }
}
